﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.Model;
using Avatar.Company;
using System.Web.Security;
using Avatar.Person;
using Avatar.Common;
using Avatar.MembershipService;
using AvatarHelper;

public partial class Job : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                if (Request.QueryString["ID"] != null)
                {
                    Guid _id = new Guid(Request.QueryString["ID"].ToString());

                    using (var _adc = DBHelper.GetDataContext())
                    {
                        var _job = _adc.Job.Where(j => j.ID == _id).FirstOrDefault();
                        if (_job != null)
                        {
                            lbl_Diploma.Text = _job.Diploma.Name;
                            lbl_JobDetail.Text = _job.Detail;
                            lbl_JobType.Text = _job.JobType.Name;
                            lbl_Location.Text = _job.Area.Name;
                            lbl_Number.Text = _job.Number.ToString();
                            var _position = _adc.Position.FirstOrDefault(p => p.ID == _job.PositionID);
                            lbl_PositionCategory.Text = _position != null ? _position.Name : "未设置";
                            lbl_PositionName.Text = _job.Title;
                            lbl_PublishDate.Text = _job.PublishDate.ToShortDateString();
                            lbl_WorkingAge.Text = _job.WorkingAge.Age;

                            if (_job.CompanyInformation != null)
                            {
                                Page.Title = _job.CompanyInformation.CompanyName + "-" + _job.Title;
                                lbl_CompanyDescription.Text = _job.CompanyInformation.Description;
                                var _industry = _adc.Industry.Where(i => i.ID == _job.CompanyInformation.Industry).FirstOrDefault();
                                lbl_CompanyIndustry.Text = _industry != null ? _industry.Name : "";
                                lbl_CompanyName.Text = _job.CompanyInformation.CompanyName;
                                lbl_CompanyName2.Text = _job.CompanyInformation.CompanyName;
                                var _size = _adc.CompanySize.Where(s => s.ID == _job.CompanyInformation.Size).FirstOrDefault();
                                lbl_CompanySize.Text = _size != null ? _size.Size : "";
                                var _nature = _adc.CompanyNature.Where(n => n.ID == _job.CompanyInformation.Nature).FirstOrDefault();
                                lbl_CompanyType.Text = _nature != null ? _nature.Nature : "";
                                lbl_Contact_CompanyName.Text = _job.CompanyInformation.CompanyName;
                            }
                            if (_job.CompanyInformation.CompanyContact.Count > 0)
                            {
                                lbl_CompanyAddress.Text = _job.CompanyInformation.CompanyContact[0].Address;
                                lbl_Contact_CompanyAddress.Text = _job.CompanyInformation.CompanyContact[0].Address;
                                lbl_Contact_CompanyTel.Text = _job.CompanyInformation.CompanyContact[0].Tel;
                                lbl_Contact_CompanyWebsite.Text = _job.CompanyInformation.CompanyContact[0].WebSite;
                            }
                        }
                    }
                }
            }catch
            {
                Helper.ShowMassage("抱歉:系统忙,请与稍后再试,或与客服联系.", "default.aspx");
            }
        }
    }
    protected void btn_Favorite_Click(object sender, EventArgs e)
    {
        LoginManager.PersonAccessCheck();
        PersonHelper.BasicInformationJobIntentionCheck();

        if (Membership.GetUser() != null)
        {
            Guid _jobID = Request.QueryString["ID"] != null ? new Guid(Request.QueryString["ID"].ToString()) : Guid.Empty;
            string _userName = Membership.GetUser().UserName;

            FavoriteManager.Favorite(_jobID, _userName);

            Response.Redirect("./PersonFavorites.aspx");
        }
        else
        {
            FormsAuthentication.RedirectToLoginPage(Request.Url.ToString());
        }
    }
    protected void btn_Apply_Click(object sender, EventArgs e)
    {
        LoginManager.PersonAccessCheck();
        PersonHelper.BasicInformationJobIntentionCheck();

        var _user=Membership.GetUser();
        if (_user != null)
        {
            try
            {
                Guid _jobID = Request.QueryString["ID"] == null ? Guid.Empty : new Guid(Request.QueryString["ID"].ToString());
                JobApplicationRecordManager.apply(_user.UserName, _jobID);

                Helper.ShowMassage("申请成功！",Request.Url.ToString());
            }
            catch (Exception _e)
            {
                Helper.ShowMassage(_e.Message, Request.Url.ToString());
            }
        }
        else
        {
            Helper.ShowMassage("您尚未登录系统，不能提交申请。请登录系统后重试。", "./login.aspx?returnURL=" + Request.Url.ToString());
        }
    }
    protected void btn_CompanyJob_Click(object sender, EventArgs e)
    {
        var _jobID = Request.QueryString["ID"] != null ? new Guid(Request.QueryString["ID"].ToString()) : Guid.Empty;
        var _job = JobManager.GetJob(_jobID);
        var _company = CompanyInformationManager.GetCompanyInformation(_job.PublisherUser);
        if (_company != null)
        {
            Response.Redirect("./CompanyJobList.aspx?company=" + Server.UrlEncode(_company.CompanyName));
        }
    }
}
